package top.igotcha.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.igotcha.eduservice.entity.EduComment;
import top.igotcha.eduservice.entity.vo.CommentQueryVo;
import top.igotcha.eduservice.service.EduCommentService;
import top.igotcha.commonutils.utils.R;

import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author Gotcha
 * @since 2021-08-15
 */
@RestController
@RequestMapping("/eduservice/educomment")
//@CrossOrigin
public class EduCommentController {
    @Autowired
    private EduCommentService commentService;

    @ApiOperation(value = "新增评论")
    @PostMapping()
    public R save(
            @ApiParam(name = "comment", value = "评论", required = true)
            @RequestBody EduComment comment){
        boolean flag = commentService.save(comment);
        if (flag){
            return R.OK();
        } else {
            return R.ERROR();
        }
    }

    @ApiOperation("根据id删除评论")
    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(name = "id" , value = "评论id" ,required = true)
            @PathVariable String id){
        boolean flag = commentService.removeById(id);
        if (flag){
            return R.OK();
        } else {
            return R.ERROR();
        }
    }

    //条件查询带分页查询评论
    @PostMapping("{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                @RequestBody(required = false) CommentQueryVo vo) {
        Page<EduComment> pageParam = new Page<>(page,limit);
        commentService.pageComentCondition(pageParam, vo);
        List<EduComment> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return  R.OK().data("total", total).data("rows", records);
    }


}

